﻿using System;
using System.Linq;
using Service;

namespace ECommerce.Controls
{
	public partial class RecommendProducts24_25 : BaseUserControl
	{
		private IProductService _ProductService;

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();
		}

		protected void RepeaterProducts_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var HyperLinkProduct = e.Item.FindControl("HyperLinkProduct") as System.Web.UI.WebControls.HyperLink;
				var dataItem = e.Item.DataItem as Data.Entity.Product;

				HyperLinkProduct.NavigateUrl = "~/ProductDetail.aspx?productid=" + dataItem.ProductID.ToString();
				HyperLinkProduct.ImageUrl = "~/PictureHandler.ashx?picturesize=170&productid=" + dataItem.ProductID;
				HyperLinkProduct.ToolTip = dataItem.Name;
			}
		}

		private void BindData()
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~/HomePage.aspx");
			}

			var languageID = SiteContext.Current.WorkingLanguage.LanguageId;

			if(languageID > 0)
			{
				if(_ProductService == null)
				{
					_ProductService = new ProductService();
				}

				var productCount = _ProductService.GetProductsByLanguage(languageID);
				if(productCount != null && productCount.Count() > 8)
				{
					var data = (from p in _ProductService.GetProductsByLanguage(languageID)
								where p.Recommend.GetValueOrDefault()
								orderby p.DisplayOrder
								select p).Skip(23).Take(2);

					RepeaterProducts.DataSource = data;
					RepeaterProducts.DataBind();
				}
			}
		}
	}
}